package com.jcf.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcf.springboot.entity.business.FilmInfo;
import com.jcf.springboot.entity.business.Play;
import com.jcf.springboot.entity.business.vo.PlayVo;
import com.jcf.springboot.service.PlayService;
import com.jcf.springboot.mapper.PlayMapper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jjj
 * @since 2021-04-03
 */
@Service
public class PlayServiceImpl extends ServiceImpl<PlayMapper, Play> implements PlayService {

    @Autowired
    private FilmInfoServiceImpl filmInfoService;
    @Override
    public List<PlayVo> getTotleList(Page<PlayVo> page) {
        return this.baseMapper.getTotleList(page);
    }

    @Override
    public void upload(InputStream inputStream) throws IOException {
        XSSFWorkbook book = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = book.getSheetAt(0);
        int cols;
        for (Row cells : sheet)
        {
            System.out.println(cells);
        }
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            XSSFRow row = sheet.getRow(i); // 表头不算
            Play play = new Play();
            QueryWrapper<FilmInfo> filmInfoQueryWrapper = new QueryWrapper<>();
            filmInfoQueryWrapper.eq("film_name",getCellStringValue(row.getCell(0)));
            FilmInfo one = filmInfoService.getOne(filmInfoQueryWrapper);
            play.setFilmId(one.getFilmId());
            play.setHallName(getCellStringValue(row.getCell(1)));
            play.setPrice(getCellBigDecimalValue(row.getCell(2)));
            play.setPlayOpenTime(getCellDateValue(row.getCell(3)));
            save(play);
        }

        book.close();
    }


    private String getCellStringValue(XSSFCell cell) {
        try {
            if (null!=cell) {
                return String.valueOf(cell.getStringCellValue());
            }
        } catch (Exception e) {
            return String.valueOf(getCellIntValue(cell));
        }
        return "";
    }

    private long getCellLongValue(XSSFCell cell) {
        try {
            if (null!=cell) {
                return Long.parseLong("" + (long) cell.getNumericCellValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    private int getCellIntValue(XSSFCell cell) {
        try {
            if (null!=cell) {
                return Integer.parseInt("" + (int) cell.getNumericCellValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    private BigDecimal getCellBigDecimalValue(XSSFCell cell) {
        try {
            if (null!=cell) {
                return BigDecimal.valueOf( cell.getNumericCellValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BigDecimal.ZERO;
    }

    private Date getCellDateValue(XSSFCell cell) {
        try {
            if (null!=cell) {
                return  cell.getDateCellValue();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
